package com.gitee.llj.shiro.config;

import com.google.code.kaptcha.servlet.KaptchaServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * <p></p>
 *
 * @Author llj
 * @Date 2018/3/20
 */
@Configuration
public class WebMvcConfig  extends WebMvcConfigurationSupport{
    @Value("${llj.session.cookie.domain}")
    private String sessionCookieDomain;
    @Value("${llj.session.cookie.name}")
    private String sessionCookieName;
    @Value("${llj.session.cookie.path}")
    private String sessionCookiePath;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true).allowedHeaders("*")
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }
    @Bean(name = "springSessionDefaultRedisSerializer")
	public DefaultCookieSerializer springSessionDefaultRedisSerializer() {
		DefaultCookieSerializer springSessionDefaultRedisSerializer = new DefaultCookieSerializer();
		springSessionDefaultRedisSerializer.setDomainName(sessionCookieDomain);
		springSessionDefaultRedisSerializer.setCookieName(sessionCookieName);
		springSessionDefaultRedisSerializer.setCookiePath(sessionCookiePath);
		return springSessionDefaultRedisSerializer;
	}
    @Bean
    public ServletRegistrationBean kaptchaServlet() {
        ServletRegistrationBean servlet = new ServletRegistrationBean(new KaptchaServlet(), "/kaptcha.jpg");
        servlet.addInitParameter("kaptcha.border", "no");
        servlet.addInitParameter("kaptcha.border.color", "105,179,90");
        servlet.addInitParameter("kaptcha.textproducer.font.color", "red");
        servlet.addInitParameter("kaptcha.image.width", "250");
        servlet.addInitParameter("kaptcha.textproducer.font.size", "70");
        servlet.addInitParameter("kaptcha.image.height", "90");
        servlet.addInitParameter("kaptcha.textproducer.char.length", "4");
        servlet.addInitParameter("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        return servlet;
    }
//    private CorsConfiguration addcorsConfig() {
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        List<String> list = new ArrayList<>();
//        list.add("*");
//        corsConfiguration.setAllowedOrigins(list);
//    /*
//    // 请求常用的三种配置，*代表允许所有，当时你也可以自定义属性（比如header只能带什么，只能是post方式等等）
//    */
//        corsConfiguration.addAllowedOrigin("*");
//        corsConfiguration.addAllowedHeader("*");
//        corsConfiguration.addAllowedMethod("*");
//        return corsConfiguration;
//    }
//    @Bean
//    public CorsFilter corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", addcorsConfig());
//        return new CorsFilter(source);
//    }

}
